<?php

namespace common\models\datas;

use common\components\bases\BaseModel;
use frontend\models\datas\OrdercourseHistory;
use common\models\datas\Customer;
use Yii;

/**
 * This is the model class for table "{{%card_attr}}".
 *
 * @property integer $id
 * @property string $card_id
 * @property string $v_code
 * @property integer $batch_id
 * @property integer $integral
 * @property string $cash
 * @property integer $customer_id
 * @property integer $active_time
 * @property integer $invalid_date
 * @property string $permission
 * @property integer $mechanic_id
 * @property integer $status
 * @property integer $site_id
 * @property integer $salesman_id
 * @property integer $issuser_id
 * @property integer $city_id
 * @property integer $sendtype
 * @property integer $number
 */
class CardAttr extends BaseModel
{
    /**
     * @inheritdoc
     */
    public $type;

    public static function tableName()
    {
        return '{{%card_attr}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['card_id', 'v_code', 'batch_id', 'integral', 'customer_id', 'active_time', 'invalid_date', 'permission', 'mechanic_id','student_id'], 'required'],
            [['batch_id', 'integral', 'customer_id', 'active_time', 'invalid_date', 'mechanic_id', 'status', 'site_id', 'salesman_id', 'issuser_id', 'city_id', 'sendtype', 'number'], 'integer'],
            [['cash'], 'number'],
            [['permission', 'type','card_no'], 'string'],
            [['card_id'], 'string', 'max' => 16],
            [['v_code'], 'string', 'max' => 32],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'card_id' => Yii::t('common', '卡号id'),
            'card_no' => Yii::t('common', '卡编号'),
            'v_code' => Yii::t('common', '激活码'),
            'batch_id' => Yii::t('common', '批次号'),
            'integral' => Yii::t('common', '积分'),
            'cash' => Yii::t('common', '现金'),
            'customer_id' => Yii::t('common', '会员编号'),
            'active_time' => Yii::t('common', '激活时间'),
            'invalid_date' => Yii::t('common', '失效时间'),
            'permission' => Yii::t('common', '权限'),
            'mechanic_id' => Yii::t('common', '所属机构'),
            'status' => Yii::t('common', '状态'),
            'site_id' => Yii::t('common', '分店编号'),
            'salesman_id' => Yii::t('common', '销售员编号'),
            'issuser_id' => Yii::t('common', '发卡人'),
            'city_id' => Yii::t('common', '城市ID'),
            'sendtype' => Yii::t('common', '发卡类型'),
            'number' => Yii::t('common', '激活码使用次数'),
            'student_id' => Yii::t('common', '学员id'),
        ];
    }

    public function getCustomer(){
        return $this->hasOne(Customer::className(),['id'=>'customer_id']);
    }

    public function fields()
    {
        return parent::fields(); // TODO: Change the autogenerated stub
        $fields['active_time'] = function(){
            return Yii::$app->formatter->asDatetime($this->active_time);
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'customer_name' => function () {
                return isset($this->customer) ? $this->customer->name : '';
            }
        ];
    }

}
